package interpreter

import (
	"kumachan/standalone/util/fatal"
	"kumachan/interpreter/compiler/loader"
	"kumachan/interpreter/compiler/checker"
	"kumachan/interpreter/compiler/generator"
	"kumachan/interpreter/runtime/vm"
)

func Run(entry_path string, options *vm.Options, repl_cfg *ReplConfig) {
	const crash_msg = "Failed to Run"
	// load source files
	var entry, _, resources, err1 = loader.LoadEntryAllowEmpty(entry_path)
	if err1 != nil { fatal.Crash(err1, crash_msg) }
	// type check
	var raw_program, err2 = checker.TypeCheck(entry)
	if err2 != nil { fatal.Crash(err2, crash_msg) }
	// generate bytecode
	var program, err3 = generator.Compile(raw_program, resources)
	if err3 != nil { fatal.Crash(err3, crash_msg) }
	// start a virtual machine
	var machine *vm.Machine
	if repl_cfg != nil {
		var ctx = ReplContext { Program: raw_program, MachinePointer: &machine }
		program.InjectEffect(repl(ctx, *repl_cfg))
	}
	vm.Execute(program, options, &machine)
}


